{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# B 12-2 多元线性回归（2）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "研究一个因变量与多个自变量的线性关系。\n",
    "\n",
    "## 案例\n",
    "\n",
    "建立血糖至于胰岛素和生长激素的二元线性回归方程\n",
    "\n",
    "### 数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X1</th>\n",
       "      <th>X2</th>\n",
       "      <th>X3</th>\n",
       "      <th>X4</th>\n",
       "      <th>X5</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>IID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>49</td>\n",
       "      <td>32.19</td>\n",
       "      <td>6.0</td>\n",
       "      <td>148</td>\n",
       "      <td>86</td>\n",
       "      <td>7.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>67</td>\n",
       "      <td>24.77</td>\n",
       "      <td>2.7</td>\n",
       "      <td>151</td>\n",
       "      <td>98</td>\n",
       "      <td>7.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>64</td>\n",
       "      <td>25.24</td>\n",
       "      <td>7.0</td>\n",
       "      <td>151</td>\n",
       "      <td>80</td>\n",
       "      <td>7.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>66</td>\n",
       "      <td>24.26</td>\n",
       "      <td>4.8</td>\n",
       "      <td>157</td>\n",
       "      <td>87</td>\n",
       "      <td>7.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>68</td>\n",
       "      <td>30.28</td>\n",
       "      <td>3.5</td>\n",
       "      <td>136</td>\n",
       "      <td>83</td>\n",
       "      <td>7.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>48</td>\n",
       "      <td>26.18</td>\n",
       "      <td>7.6</td>\n",
       "      <td>137</td>\n",
       "      <td>87</td>\n",
       "      <td>7.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>66</td>\n",
       "      <td>26.36</td>\n",
       "      <td>5.9</td>\n",
       "      <td>157</td>\n",
       "      <td>91</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>47</td>\n",
       "      <td>32.07</td>\n",
       "      <td>5.7</td>\n",
       "      <td>157</td>\n",
       "      <td>89</td>\n",
       "      <td>7.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>64</td>\n",
       "      <td>28.44</td>\n",
       "      <td>6.1</td>\n",
       "      <td>154</td>\n",
       "      <td>82</td>\n",
       "      <td>7.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>75</td>\n",
       "      <td>30.65</td>\n",
       "      <td>6.9</td>\n",
       "      <td>137</td>\n",
       "      <td>86</td>\n",
       "      <td>7.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>53</td>\n",
       "      <td>23.43</td>\n",
       "      <td>7.1</td>\n",
       "      <td>161</td>\n",
       "      <td>86</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>46</td>\n",
       "      <td>30.56</td>\n",
       "      <td>2.9</td>\n",
       "      <td>146</td>\n",
       "      <td>79</td>\n",
       "      <td>7.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>59</td>\n",
       "      <td>25.19</td>\n",
       "      <td>6.0</td>\n",
       "      <td>158</td>\n",
       "      <td>80</td>\n",
       "      <td>7.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>76</td>\n",
       "      <td>27.26</td>\n",
       "      <td>5.4</td>\n",
       "      <td>124</td>\n",
       "      <td>85</td>\n",
       "      <td>6.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>63</td>\n",
       "      <td>23.93</td>\n",
       "      <td>6.7</td>\n",
       "      <td>133</td>\n",
       "      <td>89</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>74</td>\n",
       "      <td>24.94</td>\n",
       "      <td>7.9</td>\n",
       "      <td>166</td>\n",
       "      <td>82</td>\n",
       "      <td>7.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>52</td>\n",
       "      <td>22.82</td>\n",
       "      <td>5.3</td>\n",
       "      <td>149</td>\n",
       "      <td>71</td>\n",
       "      <td>7.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>64</td>\n",
       "      <td>24.34</td>\n",
       "      <td>2.5</td>\n",
       "      <td>126</td>\n",
       "      <td>93</td>\n",
       "      <td>6.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>54</td>\n",
       "      <td>25.44</td>\n",
       "      <td>2.6</td>\n",
       "      <td>151</td>\n",
       "      <td>83</td>\n",
       "      <td>6.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>78</td>\n",
       "      <td>28.98</td>\n",
       "      <td>7.2</td>\n",
       "      <td>147</td>\n",
       "      <td>74</td>\n",
       "      <td>7.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     X1     X2   X3   X4  X5    Y\n",
       "IID                              \n",
       "1    49  32.19  6.0  148  86  7.6\n",
       "2    67  24.77  2.7  151  98  7.4\n",
       "3    64  25.24  7.0  151  80  7.4\n",
       "4    66  24.26  4.8  157  87  7.2\n",
       "5    68  30.28  3.5  136  83  7.3\n",
       "6    48  26.18  7.6  137  87  7.6\n",
       "7    66  26.36  5.9  157  91  7.5\n",
       "8    47  32.07  5.7  157  89  7.7\n",
       "9    64  28.44  6.1  154  82  7.3\n",
       "10   75  30.65  6.9  137  86  7.7\n",
       "11   53  23.43  7.1  161  86  7.5\n",
       "12   46  30.56  2.9  146  79  7.3\n",
       "13   59  25.19  6.0  158  80  7.3\n",
       "14   76  27.26  5.4  124  85  6.9\n",
       "15   63  23.93  6.7  133  89  7.5\n",
       "16   74  24.94  7.9  166  82  7.9\n",
       "17   52  22.82  5.3  149  71  7.3\n",
       "18   64  24.34  2.5  126  93  6.8\n",
       "19   54  25.44  2.6  151  83  6.9\n",
       "20   78  28.98  7.2  147  74  7.5"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"B_12_5-data.csv\", index_col=0)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 线性回归\n",
    "\n",
    "多元线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Equation of regression:\n",
      "Y = 3.876 + -0.002 * X1 + 0.032 * X2 + 0.108 * X3 + 0.008 * X4 + 0.011 * X5\n",
      "Fit result:\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>            <td>Y</td>        <th>  R-squared:         </th> <td>   0.723</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th> <td>   0.624</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th> <td>   7.317</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Mon, 09 Dec 2024</td> <th>  Prob (F-statistic):</th>  <td>0.00146</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>18:46:28</td>     <th>  Log-Likelihood:    </th> <td>  10.423</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>    20</td>      <th>  AIC:               </th> <td>  -8.846</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>    14</td>      <th>  BIC:               </th> <td>  -2.872</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     5</td>      <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "      <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Intercept</th> <td>    3.8760</td> <td>    1.011</td> <td>    3.833</td> <td> 0.002</td> <td>    1.707</td> <td>    6.045</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>X1</th>        <td>   -0.0015</td> <td>    0.004</td> <td>   -0.373</td> <td> 0.715</td> <td>   -0.010</td> <td>    0.007</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>X2</th>        <td>    0.0319</td> <td>    0.013</td> <td>    2.369</td> <td> 0.033</td> <td>    0.003</td> <td>    0.061</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>X3</th>        <td>    0.1083</td> <td>    0.025</td> <td>    4.419</td> <td> 0.001</td> <td>    0.056</td> <td>    0.161</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>X4</th>        <td>    0.0085</td> <td>    0.004</td> <td>    2.310</td> <td> 0.037</td> <td>    0.001</td> <td>    0.016</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>X5</th>        <td>    0.0106</td> <td>    0.007</td> <td>    1.596</td> <td> 0.133</td> <td>   -0.004</td> <td>    0.025</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td> 2.110</td> <th>  Durbin-Watson:     </th> <td>   2.175</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.348</td> <th>  Jarque-Bera (JB):  </th> <td>   1.051</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td>-0.012</td> <th>  Prob(JB):          </th> <td>   0.591</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 1.877</td> <th>  Cond. No.          </th> <td>4.82e+03</td>\n",
       "</tr>\n",
       "</table><br/><br/>Notes:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.<br/>[2] The condition number is large, 4.82e+03. This might indicate that there are<br/>strong multicollinearity or other numerical problems."
      ],
      "text/latex": [
       "\\begin{center}\n",
       "\\begin{tabular}{lclc}\n",
       "\\toprule\n",
       "\\textbf{Dep. Variable:}    &        Y         & \\textbf{  R-squared:         } &     0.723   \\\\\n",
       "\\textbf{Model:}            &       OLS        & \\textbf{  Adj. R-squared:    } &     0.624   \\\\\n",
       "\\textbf{Method:}           &  Least Squares   & \\textbf{  F-statistic:       } &     7.317   \\\\\n",
       "\\textbf{Date:}             & Mon, 09 Dec 2024 & \\textbf{  Prob (F-statistic):} &  0.00146    \\\\\n",
       "\\textbf{Time:}             &     18:46:28     & \\textbf{  Log-Likelihood:    } &    10.423   \\\\\n",
       "\\textbf{No. Observations:} &          20      & \\textbf{  AIC:               } &    -8.846   \\\\\n",
       "\\textbf{Df Residuals:}     &          14      & \\textbf{  BIC:               } &    -2.872   \\\\\n",
       "\\textbf{Df Model:}         &           5      & \\textbf{                     } &             \\\\\n",
       "\\textbf{Covariance Type:}  &    nonrobust     & \\textbf{                     } &             \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lcccccc}\n",
       "                   & \\textbf{coef} & \\textbf{std err} & \\textbf{t} & \\textbf{P$> |$t$|$} & \\textbf{[0.025} & \\textbf{0.975]}  \\\\\n",
       "\\midrule\n",
       "\\textbf{Intercept} &       3.8760  &        1.011     &     3.833  &         0.002        &        1.707    &        6.045     \\\\\n",
       "\\textbf{X1}        &      -0.0015  &        0.004     &    -0.373  &         0.715        &       -0.010    &        0.007     \\\\\n",
       "\\textbf{X2}        &       0.0319  &        0.013     &     2.369  &         0.033        &        0.003    &        0.061     \\\\\n",
       "\\textbf{X3}        &       0.1083  &        0.025     &     4.419  &         0.001        &        0.056    &        0.161     \\\\\n",
       "\\textbf{X4}        &       0.0085  &        0.004     &     2.310  &         0.037        &        0.001    &        0.016     \\\\\n",
       "\\textbf{X5}        &       0.0106  &        0.007     &     1.596  &         0.133        &       -0.004    &        0.025     \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "\\begin{tabular}{lclc}\n",
       "\\textbf{Omnibus:}       &  2.110 & \\textbf{  Durbin-Watson:     } &    2.175  \\\\\n",
       "\\textbf{Prob(Omnibus):} &  0.348 & \\textbf{  Jarque-Bera (JB):  } &    1.051  \\\\\n",
       "\\textbf{Skew:}          & -0.012 & \\textbf{  Prob(JB):          } &    0.591  \\\\\n",
       "\\textbf{Kurtosis:}      &  1.877 & \\textbf{  Cond. No.          } & 4.82e+03  \\\\\n",
       "\\bottomrule\n",
       "\\end{tabular}\n",
       "%\\caption{OLS Regression Results}\n",
       "\\end{center}\n",
       "\n",
       "Notes: \\newline\n",
       " [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. \\newline\n",
       " [2] The condition number is large, 4.82e+03. This might indicate that there are \\newline\n",
       " strong multicollinearity or other numerical problems."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                            OLS Regression Results                            \n",
       "==============================================================================\n",
       "Dep. Variable:                      Y   R-squared:                       0.723\n",
       "Model:                            OLS   Adj. R-squared:                  0.624\n",
       "Method:                 Least Squares   F-statistic:                     7.317\n",
       "Date:                Mon, 09 Dec 2024   Prob (F-statistic):            0.00146\n",
       "Time:                        18:46:28   Log-Likelihood:                 10.423\n",
       "No. Observations:                  20   AIC:                            -8.846\n",
       "Df Residuals:                      14   BIC:                            -2.872\n",
       "Df Model:                           5                                         \n",
       "Covariance Type:            nonrobust                                         \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "Intercept      3.8760      1.011      3.833      0.002       1.707       6.045\n",
       "X1            -0.0015      0.004     -0.373      0.715      -0.010       0.007\n",
       "X2             0.0319      0.013      2.369      0.033       0.003       0.061\n",
       "X3             0.1083      0.025      4.419      0.001       0.056       0.161\n",
       "X4             0.0085      0.004      2.310      0.037       0.001       0.016\n",
       "X5             0.0106      0.007      1.596      0.133      -0.004       0.025\n",
       "==============================================================================\n",
       "Omnibus:                        2.110   Durbin-Watson:                   2.175\n",
       "Prob(Omnibus):                  0.348   Jarque-Bera (JB):                1.051\n",
       "Skew:                          -0.012   Prob(JB):                        0.591\n",
       "Kurtosis:                       1.877   Cond. No.                     4.82e+03\n",
       "==============================================================================\n",
       "\n",
       "Notes:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "[2] The condition number is large, 4.82e+03. This might indicate that there are\n",
       "strong multicollinearity or other numerical problems.\n",
       "\"\"\""
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from statsmodels.formula.api import ols\n",
    "\n",
    "formula = 'Y ~ X1 + X2 + X3 + X4 + X5'\n",
    "\n",
    "model = ols(formula, data=df).fit()\n",
    "\n",
    "params: pd.Series = model.params\n",
    "\n",
    "print(f\"\"\"Equation of regression:\n",
    "Y = {params['Intercept']:.3f} + {params['X1']:.3f} * X1 + {params['X2']:.3f} * X2 + {params['X3']:.3f} * X3 + {params['X4']:.3f} * X4 + {params['X5']:.3f} * X5\"\"\")\n",
    "print(\"Fit result:\")\n",
    "\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由结果可知，回归直线为\n",
    "\n",
    "<!--Y = 3.876 + -0.002 * X1 + 0.032 * X2 + 0.108 * X3 + 0.008 * X4 + 0.011 * X5-->\n",
    "\n",
    "$ Y = 3.876 - 0.002 \\times X1 + 0.032 \\times X2 + 0.108 \\times X3 + 0.008 \\times X4 + 0.011 \\times X5 $\n",
    "\n",
    "### 假设检验\n",
    "\n",
    "#### 1. 模型检验\n",
    "\n",
    "用方差分析方法检验因变量与自变量之间是否存在线性回归关系。\n",
    "\n",
    "- $ H_0 $ : $ \\beta_1 = \\beta_2 = 0 $\n",
    "- $ H_1 $ : $ \\exists \\ i \\in \\{1,2\\} : \\beta_i \\neq 0 $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "F-statistic: 7.3166\n",
      "P-value: 0.0015\n",
      "Degrees of freedom: 14.0\n"
     ]
    }
   ],
   "source": [
    "f = model.fvalue\n",
    "f_p = model.f_pvalue\n",
    "dof = model.df_resid\n",
    "\n",
    "print(f\"F-statistic: {f:.4f}\")\n",
    "print(f\"P-value: {f_p:.4f}\")\n",
    "print(f\"Degrees of freedom: {dof}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$ p < 0.05 $，接受 $ H_1 $，即所求回归方程有统计学意义。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 偏回归系数检验\n",
    "\n",
    "使用 F 检验或 t 检验对每个自变量的系数进行假设检验。\n",
    "\n",
    "statsmodels 使用 t 检验。\n",
    "\n",
    "假设\n",
    "\n",
    "- $ H_0 $ : $ \\beta_j = 0,\\ j \\in \\{1,2\\} $\n",
    "- $ H_1 $ : $ \\beta_j \\neq 0,\\ j \\in \\{1,2\\} $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==== t values of regression parameters ====\n",
      "Intercept    3.833233\n",
      "X1          -0.373168\n",
      "X2           2.368681\n",
      "X3           4.419432\n",
      "X4           2.310199\n",
      "X5           1.596079\n",
      "dtype: float64\n",
      "==== p values of regression parameters ====\n",
      "Intercept    0.001827\n",
      "X1           0.714613\n",
      "X2           0.032774\n",
      "X3           0.000583\n",
      "X4           0.036634\n",
      "X5           0.132790\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(\"==== t values of regression parameters ====\")\n",
    "print(model.tvalues)\n",
    "print(\"==== p values of regression parameters ====\")\n",
    "print(model.pvalues)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$ X_1,\\ X5 > 0.05 $ ，可以认为这两个变量对回归方程没有统计学意义。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 标准化回归系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Intercept    4.579670e-16\n",
       "X1          -5.550019e-02\n",
       "X2           3.391973e-01\n",
       "X3           6.805780e-01\n",
       "X4           3.523792e-01\n",
       "X5           2.350410e-01\n",
       "dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import zscore\n",
    "\n",
    "df_std = df.select_dtypes(include=[np.number]).apply(zscore)\n",
    "\n",
    "model_std = ols(formula, data=df_std).fit()\n",
    "\n",
    "model_std.params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$ \\beta_3 > \\beta_2 > \\beta_4 > \\beta_5 > \\beta_1 $ ，说明贡献度 X3 > X2 > X4 > X5 > X1."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
